package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.LabReceiptDetailsMapper;
import com.ruoyi.system.mapper.ReceiptFormMapper;
import com.ruoyi.system.domain.LabReceiptDetails;
import com.ruoyi.system.domain.ReceiptForm;
import com.ruoyi.system.service.ILabReceiptDetailsService;
import com.ruoyi.system.utils.MoneyUtils;
import com.ruoyi.utils.LockUtil;

/**
 * receiptDetailsService业务层处理
 * 
 * @author winner pan
 * @date 2022-05-13
 */
@Service
public class LabReceiptDetailsServiceImpl implements ILabReceiptDetailsService {
	@Autowired
	private LabReceiptDetailsMapper labReceiptDetailsMapper;
	@Autowired
	private ReceiptFormMapper receiptFormMapper;
	
	private static String key = "receiptDetailsId";
	@Autowired
	private LockUtil lockUtil;

	/**
	 * 查询receiptDetails
	 * 
	 * @param detailId
	 *            receiptDetailsID
	 * @return receiptDetails
	 */
	@Override
	public LabReceiptDetails selectLabReceiptDetailsById(Long detailId) {
		return labReceiptDetailsMapper.selectLabReceiptDetailsById(detailId);
	}

	/**
	 * 查询receiptDetails列表
	 * 
	 * @param labReceiptDetails
	 *            receiptDetails
	 * @return receiptDetails
	 */
	@Override
	public ReceiptForm selectLabReceiptDetailsList(LabReceiptDetails labReceiptDetails) {
		ReceiptForm 
		form = receiptFormMapper.selectReceiptFormById(labReceiptDetails.getInstanceId());
		List<LabReceiptDetails> allItem = labReceiptDetailsMapper.selectLabReceiptDetailsList(labReceiptDetails);
		form.setDetailsList(allItem);
		return form;
	}

	/**
	 * 新增receiptDetails
	 * 
	 * @param labReceiptDetails
	 *            receiptDetails
	 * @return 结果
	 */
	@Override
	@Transactional
	public ReceiptForm insertLabReceiptDetails(LabReceiptDetails labReceiptDetails) {
		if(lockUtil.lock(this.key + labReceiptDetails.getInstanceId(), 30*1000)){
			try{
				labReceiptDetails.setTotalMoney(labReceiptDetails.getUnitPrice().multiply(BigDecimal.valueOf(labReceiptDetails.getQuantity())));
				labReceiptDetailsMapper.insertLabReceiptDetails(labReceiptDetails);
				ReceiptForm form = updateForm(labReceiptDetails, false);
				return form;
			}finally{
				lockUtil.unLock(this.key + labReceiptDetails.getInstanceId());
			}
		}else{
			throw new RuntimeException("系统忙请稍后操作");
		}
		
	}

	private ReceiptForm updateForm(LabReceiptDetails labReceiptDetails , boolean isDel) {
		LabReceiptDetails query = new LabReceiptDetails();
		query.setInstanceId(labReceiptDetails.getInstanceId());
		List<LabReceiptDetails> allItem = labReceiptDetailsMapper.selectLabReceiptDetailsList(query);
		BigDecimal sumMoney = new BigDecimal("0");
		for(LabReceiptDetails item : allItem){
			if(isDel && item.getDetailId().longValue() == labReceiptDetails.getDetailId().longValue()){
				continue;
			}
			sumMoney = sumMoney.add(item.getTotalMoney());
		}
		String uperMoney = MoneyUtils.digitCapital(sumMoney.doubleValue());
		ReceiptForm form = new ReceiptForm();
		form.setInstanceId(labReceiptDetails.getInstanceId());
		form.setTotalPrice(sumMoney);
		form.setOriginalVoucher(uperMoney);
		receiptFormMapper.updateReceiptForm(form);
		form.setDetailsList(allItem);
		return form;
	}

	/**
	 * 修改receiptDetails
	 * 
	 * @param labReceiptDetails
	 *            receiptDetails
	 * @return 结果
	 */
	@Override
	@Transactional
	public ReceiptForm updateLabReceiptDetails(LabReceiptDetails labReceiptDetails) {
		
		if(lockUtil.lock(this.key + labReceiptDetails.getInstanceId(), 30*1000)){
			try{
				labReceiptDetails.setTotalMoney(labReceiptDetails.getUnitPrice().multiply(BigDecimal.valueOf(labReceiptDetails.getQuantity())));
				labReceiptDetailsMapper.updateLabReceiptDetails(labReceiptDetails);;
				ReceiptForm form = updateForm(labReceiptDetails, false);
				return form;
			}finally{
				lockUtil.unLock(this.key + labReceiptDetails.getInstanceId());
			}
		}else{
			throw new RuntimeException("系统忙请稍后操作");
		}
		
	}

	/**
	 * 批量删除receiptDetails
	 * 
	 * @param detailIds
	 *            需要删除的receiptDetailsID
	 * @return 结果
	 */
	/*@Override
	public int deleteLabReceiptDetailsByIds(Long[] detailIds) {
		return labReceiptDetailsMapper.deleteLabReceiptDetailsByIds(detailIds);
	}*/

	/**
	 * 删除receiptDetails信息
	 * 
	 * @param detailId
	 *            receiptDetailsID
	 * @return 结果
	 */
	@Override
	@Transactional
	public ReceiptForm deleteLabReceiptDetailsById(Long detailId) {
		LabReceiptDetails labReceiptDetails = this.selectLabReceiptDetailsById(detailId);
		if(lockUtil.lock(this.key + labReceiptDetails.getInstanceId(), 30*1000)){
			try{
				labReceiptDetailsMapper.deleteLabReceiptDetailsById(detailId);
				ReceiptForm form = updateForm(labReceiptDetails, true);
				return form;
			}finally{
				lockUtil.unLock(this.key + labReceiptDetails.getInstanceId());
			}
		}else{
			throw new RuntimeException("系统忙请稍后操作");
		}
	}
}
